<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/21
 * Time: 15:09
 */

namespace app\api\controller\v2;


use app\api\lib\exception\GoodsException;
use app\api\lib\exception\MissException;
use app\api\lib\exception\SuccessMessage;
use app\api\validate\IdMustInt;
use app\api\model\Goods as GoodsModel;
use app\api\model\GoodsCategory as GoodsCategoryModel;
use app\api\model\SearchPrice as SearchPriceModel;
use app\api\model\GoodsModel as GoodsModelModel;
use app\api\model\GoodsSpecPrice as GoodsSpecPriceModel;

class GoodsController extends BaseController
{
    //首页商品推荐
    //参数num限制限时的推荐条数,默认20
    public function getRecommendForIndex()
    {
        $num = $this->request->param('num',20);
        $res =  GoodsModel::field('id,goods_name,cover_img,cover_img_origin,sale_price,market_price,sales_num')->where(['is_on_sale'=>1,'is_home_page'=>1])->limit($num)->select();
        foreach($res as $k=>$v){
            if(mb_strlen($v->getData('goods_name')) > 9){
                $v->data('goods_name',mb_substr($v->getData('goods_name'),0,9).'...');
            }
        }
        if($res){
            throw new SuccessMessage(['data' => $res]);
        }else{
            throw new GoodsException(['msg'=>'获取首页商品推荐失败!']);
        }
    }

    //用于点餐页面获取所有的的商品分类及商品
    public function getGoodsAll()
    {
        $category = GoodsCategoryModel::getGoodsOrder();
        if(!$category){
            throw new GoodsException([
                'msg'=>'获取商品分类失败!',
            ]);
        }
        foreach($category as $k=>$v){
            $res = GoodsModel::field('id,goods_name,cover_img,cover_img_origin,sale_price,market_price,sales_num,is_recommend,model_id')->where(['is_on_sale'=>1,'category'=>$v->id])->select();
            foreach($res as $key=>$val){
                if(mb_strlen($val->getData('goods_name')) > 12){
                    $val->data('goods_name',mb_substr($val->getData('goods_name'),0,12).'...');
                }
            }
            $category[$k]->data('goods',$res);
        }
        throw new SuccessMessage(['data' => $category]);
    }

    //客户没有搜索条件到搜索页的情况
    //可选参数 num 限制每次搜索的条数默认是20
    public function getNoSearchResult()
    {
        $num = $this->request->param('num',20);
        $category = GoodsCategoryModel::getGoodsOrder();
        if(!$category){
            throw new GoodsException([
               'msg'=>'获取商品分类失败!',
            ]);
        }
        array_unshift($category,['id'=>'','cates_name'=>'全部']);
//        $searchPrice = SearchPriceModel::field('price_section')->order(['id'=>'asc'])->select();
//        if(!$searchPrice){
//            throw new GoodsException([
//               'msg'=>'获取搜索价格列表失败!',
//            ]);
//        }
//        foreach($searchPrice as $k=>$v){
//            $v->data('show',$v->getData('price_section'));
//        }
//        array_unshift($searchPrice,['show'=>'全部','price_section'=>'']);
        $goods = GoodsModel::field('id,goods_name,cover_img,cover_img_origin,sale_price,sales_num,is_recommend,market_price,model_id')->where(['is_on_sale'=>1,'is_recommend'=>1])->order(['sales_num'=>'desc'])->limit($num)->select();
//        dump($goods);
        if(!$goods){
            throw new GoodsException([
               'msg'=>'获取推荐啊商品失败!',
            ]);
        }else{
            foreach($goods as $k=>$v){
                if(mb_strlen($v->getData('goods_name')) > 12){
                    $v->data('goods_name',mb_substr($v->getData('goods_name'),0,12).'...');
                }
            }
        }
//        $data = ['category'=>$category,'searchPrice'=>$searchPrice,'goods'=>$goods];
        $data = ['category'=>$category,'goods'=>$goods];
        throw new SuccessMessage(['data'=>$data]);
    }

    //用于商品的点餐搜索
    //1.参数一  name  商品名称搜索,input框的内容
    //2.参数二  category  分类搜索  分类的id
    //3.参数三  price   商品的价格区间搜索
    public function getGoodsSearch()
    {
        $params = $this->request->param();
        $name = '';
        if(!empty($params['name'])){
            $name = $params['name'];
        }
        $name = '%'.$name.'%';
        $category = [];
        if(!empty($params['category'])){
            $category = ['category'=>$params['category']];
        }
//        $price = [0,100000000];
//        if(!empty($params['price'])){
//            $price = explode('-',$params['price']);
//        }
        $res =  GoodsModel::field('id,goods_name,cover_img,cover_img_origin,sale_price,market_price,sales_num,is_recommend,model_id')->where($category)
//            ->where('sale_price','between',$price)
            ->where('goods_name','like',$name)->select();
        throw new SuccessMessage(['data'=>$res]);
    }

    //获取某个商品的的规格
    //参数  商品的id
    public function getGoodsSpec()
    {
        (new IdMustInt())->goCheck();
        $goods_id = $this->request->param('id',63);
        $model_id = (GoodsModel::field('model_id')->find($goods_id))->getData('model_id');
        $goodsModel = GoodsModelModel::with(['spec'=>function($query){$query->withField('id,name');},'spec.item'])->find($model_id)->toArray();
        $str = '';
        if(!empty($goodsModel['spec'])){
            foreach($goodsModel['spec'] as $k=>$v){
                if(!empty($v['item'])){
                    $str .= '_'.$v['item'][0]['id'];
                }
            }
        }
        $price = GoodsSpecPriceModel::where(['goods_id'=>$goods_id,'spec_item_ids'=>$str])->find();
        $data =  ['price'=>$price,'model'=>$goodsModel];
        throw new SuccessMessage(['data'=>$data]);
    }

    //获取单个商品的信息
    public function getGoodsDetails()
    {
        (new IdMustInt())->goCheck();
        $goods_id = $this->request->param('id',63);
        $data = GoodsModel::field('id,goods_name,model_id,cover_img,cover_img_origin,sale_price,sales_num,market_price,description,details,category,exemption')->where(['id'=>$goods_id,'is_on_sale'=>1])->find();
        if($data){
            throw new SuccessMessage(['data'=>$data]);
        }else{
            throw new MissException();
        }
    }

    //如果有模型则根据模型返回商品的价格
    public function getGoodsSpecPrice()
    {
        $params = $this->request->param();
        $data = GoodsSpecPriceModel::where(['goods_id'=>$params['id'],'spec_item_ids'=>$params['spec_ids']])->find();
        if($data){
            throw new SuccessMessage(['data'=>$data->getData('price')]);
        }else{
            throw new MissException();
        }
    }
}